package com.jrx.anytxn.investor.service.impl;

import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.investor.dto.loan.FundInfoRes;
import com.jrx.anytxn.investor.dto.loan.FundRouteLogRes;
import com.jrx.anytxn.investor.dto.loan.InvestorRes;
import com.jrx.anytxn.investor.dto.loan.InvestorRouteLogsRes;
import com.jrx.anytxn.investor.entity.TlFundRouteLog;
import com.jrx.anytxn.investor.entity.TlInvestorLoanApproveLog;
import com.jrx.anytxn.investor.mapper.ext.ExtTlFundRouteLogMapper;
import com.jrx.anytxn.investor.mapper.ext.ExtTlInvestorLoanApproveLogMapper;
import com.jrx.anytxn.investor.service.ITLFundRouteLogSerivce;
import com.jrx.anytxn.param.dto.investor.FundTableRes;
import com.jrx.anytxn.param.dto.investor.PrInvestorTableRes;
import com.jrx.anytxn.param.entity.PrFundFactor;
import com.jrx.anytxn.param.entity.PrInvestorTable;
import com.jrx.anytxn.param.service.investor.IFundTableService;
import com.jrx.anytxn.param.service.investor.IPrInvestorTableService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 放款资金源路由记录表Serivce实现
 *
 * @author yf
 * date 2020/6/28 0028
 * @version V1.0
 **/
@Service
public class TLFundRouteLogSerivceImpl implements ITLFundRouteLogSerivce {

    @Resource
    private ExtTlFundRouteLogMapper extTlFundRouteLogMapper;

    @Resource
    private ExtTlInvestorLoanApproveLogMapper extTlInvestorLoanApproveLogMapper;

    @Resource
    private IPrInvestorTableService prInvestorTableService;

    @Resource
    private IFundTableService fundTableService;


    @Override
    public List<FundRouteLogRes> getFundRouteLogsByFundIdAndCusId(String tenantId, String customerId, String seqNo) throws TxnException {
        List<FundRouteLogRes> res=new ArrayList<>();
        List<TlInvestorLoanApproveLog> tlInvestorLoanApproveLogList=extTlInvestorLoanApproveLogMapper.selectByCusIdAndSeqNo(tenantId,customerId,seqNo);
        Map<String,List<TlInvestorLoanApproveLog>> collect=tlInvestorLoanApproveLogList.stream().collect(Collectors.groupingBy(TlInvestorLoanApproveLog::getFundId));
        for(String key:collect.keySet()){
            //资金源路由信息
            FundRouteLogRes fundRouteLogRes=new FundRouteLogRes();
            //出资方路由信息
            List<InvestorRouteLogsRes> investorRouteLogsResList =new ArrayList<>();
            FundTableRes fundTableRes=fundTableService.findByFundIdAndTenId(tenantId,key);
            List<TlInvestorLoanApproveLog> tlInvestorLoanApproveLogs=collect.get(key);
            fundRouteLogRes.setFundId(fundTableRes.getFundId());
            fundRouteLogRes.setChineseName(fundTableRes.getChineseName());
            for (TlInvestorLoanApproveLog tlInvestorLoanApproveLog:tlInvestorLoanApproveLogs) {
                InvestorRouteLogsRes investorRouteLogsRes=new InvestorRouteLogsRes();
                PrInvestorTableRes prInvestorTableRes=prInvestorTableService.findByTenIdAndInvestorId(tenantId,tlInvestorLoanApproveLog.getInvestorId());
                investorRouteLogsRes.setInvestorId(prInvestorTableRes.getInvestorId());
                investorRouteLogsRes.setChineseName(prInvestorTableRes.getChineseName());
                investorRouteLogsRes.setApproveStatus(tlInvestorLoanApproveLog.getApproveStatus());
                investorRouteLogsResList.add(investorRouteLogsRes);
                fundRouteLogRes.setCreateTime(tlInvestorLoanApproveLog.getCreateTime());
            }
            fundRouteLogRes.setInvestorRouteLogsList(investorRouteLogsResList);
            res.add(fundRouteLogRes);
        }
        return res;
    }

    @Override
    public FundInfoRes getFundAndInvestorInfo(String tenantId, String seqNo, String customerId)throws TxnException {
        FundInfoRes res=new FundInfoRes();
        BigDecimal loanAmount=BigDecimal.ZERO;
        List<InvestorRes> investorResList=new ArrayList<>();
        List<TlInvestorLoanApproveLog> tlInvestorLoanApproveLogList=extTlInvestorLoanApproveLogMapper.selectByCusIdAndSeqNo(tenantId,customerId,seqNo);
        if(CollectionUtils.isEmpty(tlInvestorLoanApproveLogList)){
            return null;
        }
        FundTableRes fundTableRes=fundTableService.findByFundIdAndTenId(tenantId,tlInvestorLoanApproveLogList.get(0).getFundId());
        res.setFundId(fundTableRes.getFundId());
        res.setChineseName(fundTableRes.getChineseName());
        for (TlInvestorLoanApproveLog tl:tlInvestorLoanApproveLogList) {
            //查询出资方信息
            PrInvestorTable prInvestorTable=prInvestorTableService.findInvestorByInvestorId(tl.getInvestorId(),tenantId);
            InvestorRes investorRes=new InvestorRes();
            investorRes.setInvestorId(tl.getInvestorId());
            investorRes.setLoanAmount(tl.getLoanAmount());
            investorRes.setChineseName(prInvestorTable.getChineseName());
            investorResList.add(investorRes);
            loanAmount=loanAmount.add(tl.getLoanAmount());
        }
        res.setLoanAmount(loanAmount);
        res.setInvestorResList(investorResList);
        return res;
    }
}
